#!/bin/sh

set -e

# NOTE: DevHelp authors change the book location every minor update, so YMMV...
XDG_DATA_HOME=${XDG_DATA_HOME:=$HOME/.local/share}
DEVHELP="$XDG_DATA_HOME/devhelp"

for FILE
do
	BASENAME=`basename "$FILE"`
	BOOK=${BASENAME%.*}
	
	mkdir -p "$DEVHELP/books"
	rm -rf "$DEVHELP/books/$BOOK"
	
	TMPDIR=`mktemp -dt devhelp.XXXXXX`

	tar -xzf "$FILE" -C "$TMPDIR"

	mv "$TMPDIR/book" "$DEVHELP/books/$BOOK"
	mv "$TMPDIR/book.devhelp" "$DEVHELP/books/$BOOK/$BOOK.devhelp"

	rm -rf "$TMPDIR"
done
